package com.jianshuge.app.bean;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.jianshuge.app.AppException;
import com.jianshuge.app.common.StringUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 好友列表实体类
 */
public class FriendList extends Entity{

	public final static int TYPE_FANS = 0x00;
	public final static int TYPE_FOLLOWER = 0x01;
	
	private List<Friend> friendlist = new ArrayList<Friend>();
	
	private int page_size;
	private int friend_count;

	
	public int getPageSize() {
		return page_size;
	}
	public int getFriendCount() {
		return friend_count;
	}
	public List<Friend> getFriendList() {
		return friendlist;
	}
	
	/**
	 * 好友实体类
	 */
	public static class Friend implements Serializable {
		private int userid;
		private String name;
		private String face;
		private String expertise;
		private String location;

		public int getUserid() {return userid;}
		public void setUserid(int userid) {this.userid = userid;}
		public String getName() {return name;}
		public void setName(String name) {this.name = name;}
		public String getFace() {return face;}
		public void setFace(String face) {this.face = face;}
		public String getExpertise() {return expertise;}
		public void setExpertise(String expertise) {this.expertise = expertise;}
		public String getLocation() {return location;}
		public void setLocation(String location) {this.location = location;}		
	}

	public List<Friend> getFriendlist() {
		return friendlist;
	}
	public void setFriendlist(List<Friend> resultlist) {
		this.friendlist = resultlist;
	}
	
	public static FriendList parse(InputStream inputStream) throws IOException, AppException {
		FriendList friendlist = new FriendList();
		Friend friend = null;

		String inputStr;
		BufferedReader streamReader = new BufferedReader( new InputStreamReader(inputStream) ); 
	    StringBuilder responseStrBuilder = new StringBuilder();
	    while ((inputStr = streamReader.readLine()) != null)
	        responseStrBuilder.append(inputStr);
	    String strResult = responseStrBuilder.toString();
	    
		try {
			
	    	JSONArray json_arr = new JSONArray(strResult);
			
	    	friendlist.page_size = json_arr.length();
	    	friendlist.friend_count = json_arr.length();
			for (int i = 0; i < json_arr.length(); i++) {
				friend = new Friend();
				JSONObject friend_json_obj = json_arr.optJSONObject(i);
				friend.setUserid( friend_json_obj.getInt("id") );
				friend.setName(friend_json_obj.getString("user_name"));
				friend.setLocation(friend_json_obj.getString("user_location"));
				friend.setExpertise(friend_json_obj.getString("user_summary"));
				friend.setFace(friend_json_obj.getString("user_avatar"));
				friendlist.getFriendlist().add(friend); 
				friend = null; 
			}

		} catch (JSONException e) { 
            System.out.println("Jsons parse error !"); 
            e.printStackTrace(); 
        }
		
		return friendlist; 

	}
}
